package com.shipwe.loans.utils.helper

import com.shipwe.loans.MyApps
import com.shipwe.loans.R
import org.json.JSONObject
import java.util.Calendar

/**
 * ShipweDateHelper
 * @Author wwdeng
 * @CreateTime: 2024/10/22 10:13
 * @Annotation：日期帮助类
 */
object ShipweDateHelper {

    /**
     * 月份
     */
    private val MONTH = arrayListOf(R.string.month1,R.string.month2,R.string.month3,R.string.month4,R.string.month5,R.string.month6,R.string.month7,R.string.month8,R.string.month9,R.string.month10,R.string.month11,R.string.month12)

    /**
     * 周（从 周日 ~ 周六）
     */
    private val WEEK = arrayListOf(R.string.week7,R.string.week1,R.string.week2,R.string.week3,R.string.week4,R.string.week5,R.string.week6)

    /**
     * 获取月份列表
     */
    fun getMonthList(): ArrayList<JSONObject>{
        val monthList = arrayListOf<JSONObject>()
        MONTH.forEach {
            val item  = JSONObject()
            item.put("month",MyApps.app.getString(it))
            monthList.add(item)
        }
        return monthList
    }

    /**
     * 获取周列表
     */
    fun getWeekList(): ArrayList<String>{
        val weekList = arrayListOf<String>()
        WEEK.forEach {
            weekList.add(MyApps.app.getString(it))
        }
        return weekList
    }

    /**
     * 根据月份获取的天列表
     */
    fun getDayList(year: Int,month: Int): ArrayList<JSONObject>{
        val dayList = arrayListOf<JSONObject>()
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month - 1)
        val daysCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        for (i in 0..<daysCount){
            val item  = JSONObject()
            item.put("year",year)
            item.put("month",month)
            item.put("day",i+1)
            dayList.add(item)
        }
        return dayList
    }

    /**
     * 获取指定日期是星期几
     *  1 - 7 ： 星期日 - 星期六
     */
    fun getDayOfWeek(year: Int,month: Int,day: Int): Int{
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month - 1)
        calendar.set(Calendar.DAY_OF_MONTH,day)
        return calendar.get(Calendar.DAY_OF_WEEK) - 1
    }

    /**
     * 获取当前年份
     */
    fun getCurtYear(): Int{
        return Calendar.getInstance().get(Calendar.YEAR)
    }

    /**
     * 获取当前月份
     */
    fun getCurtMonth(): Int{
        return Calendar.getInstance().get(Calendar.MONTH) + 1
    }

    /**
     * 获取当前日期
     */
    fun getCurtDay(): Int{
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
    }

    /**
     * 指定年 上一年
     */
    fun getLastYear(year: Int = Calendar.getInstance().get(Calendar.YEAR)): Calendar{
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.add(Calendar.YEAR,-1) // 减去一年
        return calendar
    }

    /**
     * 指定年 下一年
     */
    fun getNextYear(year: Int = Calendar.getInstance().get(Calendar.YEAR)): Calendar{
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.add(Calendar.YEAR,+1) // 减去一年
        return calendar
    }

    /**
     * 获取指定年月 上一个月
     */
    fun getLastMonth(year: Int,month: Int): Calendar{
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month-1)
        calendar.add(Calendar.MONTH,-1)
        return calendar.clone() as Calendar
    }

    /**
     * 获取指定年月 下一个月
     */
    fun getNextMonth(year: Int,month: Int): Calendar{
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR,year)
        calendar.set(Calendar.MONTH,month-1)
        calendar.add(Calendar.MONTH,+1)
        return calendar.clone() as Calendar
    }
}